﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Linq;
using WPFExcelReport.Helpers;
using System.Windows.Input;
using System.Windows.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// 专用于坯布检验
    /// 1.扫描布卷号，进入验布，读取生产单信息
    /// 2.输入长度，修改数据库记录
    /// 3.剪出新布卷
    /// 4.打印标签
    /// 5.疵点保存修正
    /// 6.事件记录
    /// 7.纬纱扣除
    /// 8.织轴扣除
    /// </summary>
    public partial class TWB19X
    {
        public int pos = 1;
        public TWB19X()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBFEHL();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
        } 
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length == 0)
                return;
            KRS = EXAUF.Substring(0, 2);
            LFD = EXAUF.Substring(2, 6);
            POS = EXAUF.Substring(8, 4);
            ERVER = int.Parse(EXAUF.Substring(12));
            PM["RWKRS"] = KRS;
            PM["RWLFD"] = LFD;
            PM["RWPOS"] = POS;
            PM["ERVER"] = ERVER;
            SResult = TMV.LOADERVER(PM);
            MergeData(SResult, false);
            SetUpChart();
            if (!IsAuto)
                return;
            if (TBMain.Rows.Count == 0)
            {
                pos = 0;
            }
            else
            {
                var rm = (from DataRow row in TBMain.AsEnumerable()
                          select Convert.ToInt16(row["WBFFPOS"].ToString()))
                 .Max();
                pos = int.Parse(rm.ToString());
            }
            pos++;
            VAR = pos.ToString();
            PM["ERVER"] = ERVER;
            PM["FPOS"] = CPCS * 100 + pos;
            PM["QPKT"] = 0;
            TMV.NEWERROR(PM);
            vwMaster.View.MoveCurrentToFirst();
            base.OnEXAUFChg(vlaue);
        }
        private void KEYUP(object sender, KeyEventArgs e)
        {
            QueryCommand(sender, e);
        }
        
        public override void OnEKEYChg()
        {
            if (EKEY == "")
                return;
            base.OnEKEYChg();
            string lgerror = EKEY.Substring(10, 2);
            if (lgerror == "01")
            {
                if (IsLengStart == false)
                {
                    IsLengStart = true;
                }
                else
                {
                    IsLengStart = false;
                    if (IsCanNew
                        & IsAutoNext)
                    {
                        EndError(null, null);

                        NewCommand(null, null);
                    }

                }
            }
            else
            {
                IsLengStart = false;
                if (IsCanNew 
                    & IsAutoNext)//是否等待输入
                {
                    EndError(null, null);
                    NewCommand(null, null);
                }
            }
            SetUpChart();
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    return;
                if (EKEY.Length == 0)
                {
                    // throw new Exception("请选择疵点");
                }

                TMV.UpdateWBFEHL(dwMaster, 0);

                if (VAR == "")
                    VAR = "0";
                pos = (int.Parse(VAR));
                pos++;
                VAR = pos.ToString();
                PM["RWKRS"] = KRS;
                PM["RWLFD"] = LFD;
                PM["RWPOS"] = POS;
                PM["ERVER"] = ERVER;
                PM["FPOS"] = CPCS * 100 + pos;
                PM["QPKT"] = 0;
                TMV.NEWERROR(PM);
                vwMaster.View.MoveCurrentToFirst();
                base.NewCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex.Message);
            }
        }

        public override void OnNEWFError()
        {
            if (dwNFError == null)
                return; 

            dwData = TBMain.NewRow();
            dwData.ItemArray = dwNFError.ItemArray;
            dwData["WBFRWKRS"] = KRS;
            dwData["WBFRWLFD"] = LFD;
            dwData["WBFRWPOS"] = POS;
            dwData["WBFVERSION"] = ERVER;
            dwData["WBFFPOS"] = dwData["WBFFPOS"].ToString();
            dwData["WBFLGVON"] = CMTR + double.Parse(dwData["WBFLGVON"].ToString());
            dwData["WBFLGBIS"] = CMTR + double.Parse(dwData["WBFLGBIS"].ToString());
            filter = "WBFRWKRS+WBFRWLFD+WBFRWPOS+WBFVERSION+WBFFPOS='" + KRS + LFD+POS+ERVER+ dwData["WBFFPOS"].ToString() +"'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                TMV.DWFE01(row);
                row.Delete();
            }
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.DWFE01(dwData);
            TMV.InsertWBFEHL(dwData);
            base.OnNEWFError();
        } 
    }
}
